//
//  XGLMessageRequestAPI.swift
//  XGamesLive
//
//  Created by ios on 2024/5/28.
//

import Foundation
import PanKit
import Moya
import SwiftyRSA

class XGLMessageRequestAPI: PanRequestBaseAPI,XGL_RequestConfigurationProtocol {
    enum RequestAPI{
        case ValidationSensitiveWord(content: String)
    }
    
    init(requestAPI: RequestAPI) {
        super.init()
        self.resuestAPI = requestAPI
        self.requestConfiguration()
    }
    private var resuestAPI: RequestAPI!
    
    override var path: String{
        switch self.resuestAPI {
        case .ValidationSensitiveWord:
            return "/BerryApi/chat/checkSensitive"
        default:
            return ""
        }
    }
    
    override var method: Moya.Method{
        switch self.resuestAPI {
        case .ValidationSensitiveWord:
            return .post
        default:
            return .get
        }
    }
    
    override var task: Task{
        switch self.resuestAPI {
        case .ValidationSensitiveWord:
            return .requestParameters(parameters: parameters, encoding: JSONEncoding.default)
        default:
//            if parameters.isEmpty == false{
//                let parameStr = parameters.map { "\($0.key)=\($0.value)" }.joined(separator: "&")
//                let rsaParams = XGL_PRSA.encryptString(parameStr, publicKey: XGL_EncipherTool.share.cpk)
//                return .requestParameters(parameters: ["encryptParam": rsaParams ?? ""],encoding: parameterEncoding)
//            }
            if parameters.isEmpty == false{
                let parameStr = parameters.map { "\($0.key)=\($0.value)" }.joined(separator: "&")
                do {
                    let publicKey = try PublicKey(base64Encoded: XGL_EncipherTool.share.cpk)
                    let encryptedStr = try ClearMessage(string: parameStr, using: .utf8).encrypted(with: publicKey, padding: .PKCS1)
                    return .requestParameters(parameters: ["encryptParam": encryptedStr.base64String],encoding: parameterEncoding)
                } catch {
                print(error)
                }
            }
            return .requestParameters(parameters: parameters,encoding: parameterEncoding)
        }
    }
    
    override var cacheModel: PanRequestBaseAPI.PanRequestCacheModel{
        return .none
    }
    
    override var cacheKey: String{
        return "test"
    }
    
    override var parameters: [String : Any]{
        var dic = super.parameters
        switch self.resuestAPI {
        case .ValidationSensitiveWord(let content):
            dic["content"] = content
            return dic
        default:
            return dic
        }
    }
}

